package com.zcj.demo.config;

import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 68300119
 * @Date 2021/3/1
 **/
@Configuration
public class ShiroConfig {


    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(
            DefaultWebSecurityManager defaultWebSecurityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor =
                new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * 1、创建Realm对象 自定义类 继承AuthorizingRealm
     */
    @Bean("userRealm")
    public UserRealm userRealm(@Qualifier("bCryptCredentialsMatcher") BCryptCredentialsMatcher bCryptCredentialsMatcher) {
        UserRealm userRealm = new UserRealm();
        // 设置密码校验方式
        userRealm.setCredentialsMatcher(bCryptCredentialsMatcher);
        return userRealm;
    }

    /**
     * 2、DefaultWebSecurityManager
     */
    @Bean("defaultWebSecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        // 关联Realm
        manager.setRealm(userRealm);
        return manager;
    }

    /**
     * 3、ShiroFilterFactoryBean
     */
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Qualifier("defaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);
        /**
         * 添加shiro内置过滤器
         * anon:无需认证就可以访问
         * authc:必须认证才可以访问
         * user:必须拥有 记住我 才能访问
         * perms: 必须拥有对某个资源的权限才能访问
         * roles: 必须拥有某个角色权限才能访问
         */
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/api/login", "anon");

        filterMap.put("/api/users/all", "roles[admin]");
        //授权
        bean.setFilterChainDefinitionMap(filterMap);
        return bean;
    }

    @Bean("bCryptCredentialsMatcher")
    public BCryptCredentialsMatcher getBCryptCredentialsMatcher() {
        return new BCryptCredentialsMatcher();
    }
}
